package com.zeh.billing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zeh.billing.config.Arith;
import com.zeh.billing.config.BasePage;
import com.zeh.billing.config.CommonResult;
import com.zeh.billing.config.PageUtil;
import com.zeh.billing.entity.*;
import com.zeh.billing.mapper.CmoneyMapper;
import com.zeh.billing.mapper.CuscardMapper;
import com.zeh.billing.mapper.CustomerMapper;
import com.zeh.billing.mapper.DiscountMapper;
import com.zeh.billing.service.CustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zeh
 * @since 2021-08-20
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {

    @Autowired
    private  CustomerMapper customerMapper;

    @Autowired
    private CmoneyMapper cmoneyMapper;

    @Autowired
    private CuscardMapper cardMapper;

    @Autowired
    private DiscountMapper discountMapper;

    @Override
    public CommonResult add(CustomerAddDo customerdo) {
        Customer customer = new Customer();
        BeanUtils.copyProperties(customerdo,customer);
        int insert = customerMapper.insert(customer); //客户信息
        if(insert == 1){ //客户插入成功
            if(customerdo.getCardid()!=""){
                System.out.println("进入");
                Cuscard cuscard = new Cuscard();
                BeanUtils.copyProperties(customerdo,cuscard);
                int insert2 = cardMapper.insert(cuscard);
                if(insert2 == 1){
                    return CommonResult.success("新增顾客成功！");
                }else{
                    return CommonResult.failed("新增失败，用户卡信息新增失败！");
                }
            }
            return CommonResult.success("新增顾客次卡成功！");
        }
        return CommonResult.failed("新增失败！");

    }

    @Override
    public CommonResult search(String cid) {
        Customer customer = customerMapper.selectOne(new QueryWrapper<Customer>().lambda().eq(Customer::getCustomerid, cid));
//        int type =customer.getType();
//        Discount discount = discountMapper.selectOne(new QueryWrapper<Discount>().lambda().eq(Discount::getType, type));
        CustomerVo customerVo = new CustomerVo();
        List<Cuscard> cuscards = cardMapper.selectList(new QueryWrapper<Cuscard>().lambda().eq(Cuscard::getCustomerid, customer.getCustomerid()));//得到卡列表
//        for(Cuscard cards:cuscards){
//            String type = cards.getType();//得到类型
//            discountMapper.selectOne()
//        }
        BeanUtils.copyProperties(customer,customerVo);
        customerVo.setCuscards(cuscards);
        return CommonResult.success(customerVo);
    }

    @Override
    @Transactional
    public boolean chargeMoney(CustomerChargeDto chargeDto) {
        int numcard = 0;
        Customer customer1 = customerMapper.selectOne(new QueryWrapper<Customer>().lambda().eq(Customer::getCustomerid, chargeDto.getCustomerid())); //通过顾客id得到顾客
        //============================次卡操作==============================
        Customer customer = new Customer();
        if(chargeDto.getNumcard()>0){ //使用次卡
            numcard = customer1.getNumcard() - 1;  //次卡数减一
            if(numcard <0){ //次卡数不足
                return false;
            }
            Cmoney cmoney = new Cmoney();
            cmoney.setCtype(1);//消费
            cmoney.setLastcard(1); //消费次卡数
            cmoney.setNowcard(numcard); //当前的次卡数
            cmoney.setCid(chargeDto.getCustomerid()); //用户id
            BeanUtils.copyProperties(customer1,customer);
            customer.setNumcard(numcard);
            int b = customerMapper.updateById(customer);
            int insert = cmoneyMapper.insert(cmoney);
            if(insert ==1 && b==1){
                return true;
            }else{
                return false;
            }
        }else {
            //============================打折卡操作============================== //次卡不足,使用打折卡
            Double money  = chargeDto.getMoney();//消费钱
            Cuscard cuscard = cardMapper.selectOne(new QueryWrapper<Cuscard>().lambda().eq(Cuscard::getCardid,  chargeDto.getCardid()));//得到卡信息
            if(cuscard==null){ //无卡信息
                return false;
            }
            Double money1 = cuscard.getMoney(); //现在钱数
            int ctype = cuscard.getType();//卡类型

            CustomerChargeDto customer2 = new CustomerChargeDto();
            double mul = Arith.mul(money, 0.80); //8折
            double mul1 = Arith.mul(money, 0.70); // 7折
//     if( money < money1){ //消费数高于金额数
//          return false; //金额不足
//      }
            Double result ;
            Cmoney cmoney = new Cmoney();
            Cuscard cuscard1 = new Cuscard(); //更改卡的对象
            switch (ctype) {
                case 1: //不打折
                    result = Arith.sub(money1, money);
                    if(result<0){
                        return false;
                    }
                    customer2.setMoney(Arith.sub(money1, money));
                    cmoney.setHismoney(money); //历史消费
                    break;
                case 2: //8折
                    result = Arith.sub(money1, mul);
                    if(result<0){
                        return false;
                    }
                    customer2.setMoney(Arith.sub(money1, mul));
                    cmoney.setHismoney(mul); //历史消费
                    break;
                case 3: //7折
                    result = Arith.sub(money1, mul1);
                    if(result<0){
                        return false;
                    }
                    customer2.setMoney(Arith.sub(money1, mul1));
                    cmoney.setHismoney(mul1); //历史消费
                    break;
            }
            cmoney.setCtype(1);//消费
            cmoney.setCardid(cuscard.getCardid()); //打折卡id
            cmoney.setLastmoney(customer2.getMoney()); //当前剩余
            cmoney.setCid(chargeDto.getCustomerid());//用户id
            BeanUtils.copyProperties(cuscard,cuscard1);
            cuscard1.setMoney( customer2.getMoney() );   //当前剩余钱数
            int i = cardMapper.updateById(cuscard1);
            int insert = cmoneyMapper.insert(cmoney);
            if(insert != 0 && i!=0){
                return  true;
            }else {
                return  false;
            }
        }
        // 卡号 用户id 姓名 钱数



    }

    @Override
    @Transactional
    public boolean addMoney(addMoneyDto addDto) {  //通过顾客编号增加次卡数
        Cmoney cmoney = new Cmoney(); //充值记录
        Customer customer2 = new Customer();
        if(addDto.getNumcard()!=0){ //==============================充值次卡======================
            Customer customer1 = customerMapper.selectOne(new QueryWrapper<Customer>().lambda().eq(Customer::getCustomerid, addDto.getCustomerid()));//得到顾客
             cmoney.setCtype(0);//充值
             int card2 = customer1.getNumcard(); //原来的次卡数
            card2 = card2 + addDto.getNumcard();  //现在的次卡数
            cmoney.setCid(customer1.getCustomerid()); //用户编号
            cmoney.setLastcard(addDto.getNumcard());//增加的次卡数
            cmoney.setNowcard(card2); //现在的次卡数
             BeanUtils.copyProperties(customer1,customer2);
             customer2.setNumcard(card2);
           // boolean b = customerMapper.updateMoney(customer1.getId(),null,card2);//充值次卡
            int b = customerMapper.updateById(customer2);// 更改次卡数
            int insert = cmoneyMapper.insert(cmoney);
            if(insert != 0 && b!=0){
                return  true;
            }
            return false;
        }else{//==============================充值卡号的钱====================== 通过卡号增加钱
             Cuscard cusCard = cardMapper.selectOne(new QueryWrapper<Cuscard>().lambda().eq(Cuscard::getCardid, addDto.getCardid()));//得到卡
             cmoney.setCtype(0);//充值
             cmoney.setCid(cusCard.getCustomerid()); //用户编号
             cmoney.setHismoney(addDto.getMoney()); //当前充值钱数
             Double  money = cusCard.getMoney() + addDto.getMoney(); //现在的金额
             cmoney.setLastmoney(money); //当前金额
            int insert = cmoneyMapper.insert(cmoney)    ;
            if(insert != 0  ){
                return  true;
            }
           return false;
        }
    }

    @Override
    public CommonResult searchAll(BasePage basePage) {
        List<Customer> customers = customerMapper.selectList(null); //查询出所有的客户
        List<CustomerVo> customerVos = new ArrayList<>();
        for (Customer customer:customers){//遍历客户
          String customerid = customer.getCustomerid(); //用户编号
            List<Cuscard> cuscards = cardMapper.selectList(new QueryWrapper<Cuscard>().lambda().eq(Cuscard::getCustomerid, customerid)); //卡号
            CustomerVo customerVo = new CustomerVo();
            BeanUtils.copyProperties(customer,customerVo);
            customerVo.setCuscards(cuscards);
            customerVos.add(customerVo);
        }

        List<CustomerVo> customerVos1 = PageUtil.DPage(customerVos, basePage.getCurrentPage(), basePage.getPageSize()); //分页
        return CommonResult.success(customerVos1);
    }

    @Override
    @Transactional
    public Integer delete(String Cid) { //根据编号删除客户
        Customer customer = customerMapper.selectOne(new QueryWrapper<Customer>().lambda().eq(Customer::getCustomerid, Cid)); //客户
        List<Cuscard> cuscards = cardMapper.selectList(new QueryWrapper<Cuscard>().lambda().eq(Cuscard::getCustomerid, customer.getCustomerid()));//卡号
        int delete = customerMapper.delete(new QueryWrapper<Customer>().lambda().eq(Customer::getCustomerid, Cid)); //删除客户
        if(cuscards.size()!=0){ //有卡号
            List<Integer> total = new ArrayList<>();
            for (Cuscard cardss : cuscards) {
                total.add(cardss.getId());
            }
            int i = cardMapper.deleteBatchIds(total);//删除卡号
            if(delete==1 && i==1){
                return 1;
            }
        }
        if(delete == 1){
            return 1;
        }
      return  0;
    }

    @Override
    public boolean addCard(addCardDo cardDo) {
        Cuscard cuscard = new Cuscard();
        BeanUtils.copyProperties(cardDo,cuscard);
        int insert = cardMapper.insert(cuscard);
        if(insert==1){
            return true;
        }
        return false;
    }

    @Override
    public List<CustomerVo> searchCustomer(SearchCustomerDo customerDo) {
        List<CustomerVo> customerVos =  customerMapper.selectCustomer(customerDo);
        return customerVos;
    }
}
